<script setup lang="ts">
import { getHomeGoodsGuessLikeAPI } from '@/services/home'
import type { PageParams } from '@/types/global'
import type { GuessItem } from '@/types/home'
import { onMounted,ref } from 'vue'
import { useI18n } from 'vue-i18n'
const { t,locale } = useI18n()
// 分页参数
const pageParams: Required<PageParams> = {
  page: 1,
  pageSize: 10,
}
const messageDel = ref()
// 猜你喜欢的列表
const guessList = ref<GuessItem[]>([])
// 已结束标记
const finish = ref(false)
// 获取猜你喜欢数据
const getHomeGoodsGuessLikeData = async () => {
  // 退出分页判断
  if (finish.value === true) {
    return uni.showToast({ icon: 'none',title: t('no.more.data') + '~' })
  }
  const res = await getHomeGoodsGuessLikeAPI(pageParams)
  // guessList.value = res.result.items
  // 数组追加
  guessList.value.push(...res.result.items)
  // 分页条件
  if (pageParams.page < res.result.pages) {
    // 页码累加
    pageParams.page++
  } else {
    finish.value = true
  }
}
const dialogClose = () => {
  messageDel.value.close()
}
// 重置数据
const resetData = () => {
  pageParams.page = 1
  guessList.value = []
  finish.value = false
}
const payItem = ref()
// 收款
const payCb = (item: any) => {
  payItem.value = item
  messageDel.value.open()
}
const payConfirm = () => {
  console.log('payConfirm',payItem.value)
  messageDel.value.close()
}
// 组件挂载完毕
onMounted(() => {
  getHomeGoodsGuessLikeData()
})
// 暴露方法
defineExpose({
  resetData,
  getMore: getHomeGoodsGuessLikeData,
})
</script>

<template>
  <view class="order-">
    <view class="order-item" v-for="item in guessList" :key="item.id">
      <view class="order-no flex-rbt">
        <text class="order-no-text">MS2024061801392130192</text>
        <view class="order-no-desc flex-rbt">
          <icon type="success" :size="18" color="red" />
          <text class="order-no-desc-text">{{ t('receipt.havepaid') }}</text>
        </view>
      </view>
      <view class="item-line">{{ t('receipt.Merchanttrackingnumber') }}：{{ item.name }}</view>
      <view class="item-line">{{ t('receipt.patternofpayment') }}：{{ item.name }}</view>
      <view class="item-line">{{ t('receipt.Receiptor') }}：{{ item.name }}</view>
      <view class="item-line">{{ t('receipt.shroffaccount') }}：{{ item.name }}</view>
      <view class="item-line">{{ t('receipt.ordertime') }}：{{ item.name }}</view>
      <view class="item-line">{{ t('receipt.Paymenttime') }}：{{ item.name }}</view>
      <view class="price- flex-rbt">
        <view class="price-n">￥10</view>
        <view class="right-wrap flex-rbt">
          <view class="price-status">{{ t('receipt.tobesettled') }}</view>
          <button @click="payCb(item)" type="primary" size="mini" class="button">
            {{ t('receipt.gathering') }}
          </button>
        </view>
      </view>
    </view>
  </view>
  <view>
    <uni-popup ref="messageDel" type="dialog">
      <uni-popup-dialog type="warn" :cancelText="t('input.cancel')" :confirmText="t('input.confirm')" title=""
        :content="t('receipt.Are you sure you want to collect the money') + '?'" @confirm="payConfirm"
        @close="dialogClose"></uni-popup-dialog>
    </uni-popup>
  </view>
  <view class="loading-text">
    {{ finish ? t('no.more.data') + '~' : t('more.loading') + '...' }}
  </view>
</template>

<style scoped lang="scss">
:host {
  display: block;
}

.order- {
  display: flex;
  flex-wrap: wrap;
  flex-direction: column;
  justify-content: space-between;
  padding: 0 20rpx;

  .order-item {
    width: 100%;
    background-color: #fff;
    margin-bottom: 10rpx;
    border-radius: 8rpx;
    padding-left: 22rpx;
    padding-right: 22rpx;
    padding-top: 8rpx;
    padding-bottom: 15rpx;

    .order-no {
      padding-top: 8px;
      padding-bottom: 8px;
      border-bottom: 1rpx solid #ccc;
      margin-bottom: 10rpx;

      .order-no-desc {
        .order-no-desc-text {
          font-size: 26rpx;
          margin-left: 4rpx;
          color: red;
        }
      }
    }

    .item-line {
      height: 50rpx;
      line-height: 50rpx;
      font-size: 28rpx;
    }

    .price- {
      padding-top: 20rpx;
      height: 70rpx;
      border-top: 1rpx solid #ccc;

      .right-wrap {
        .button {
          margin-left: 10rpx;
        }
      }

      .price-n {
        color: red;
        font-size: 36rpx;
      }

      .price-status {
        color: orange;
        font-size: 26rpx;
      }
    }
  }

  .image {
    width: 304rpx;
    height: 304rpx;
  }

  .name {
    height: 75rpx;
    margin: 10rpx 0;
    font-size: 26rpx;
    color: #262626;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
  }

  .price {
    line-height: 1;
    padding-top: 4rpx;
    color: #cf4444;
    font-size: 26rpx;
  }

  .small {
    font-size: 80%;
  }
}

// 加载提示文字
.loading-text {
  text-align: center;
  font-size: 28rpx;
  color: #666;
  padding: 20rpx 0;
}
</style>
